package com.neusoft.service.impl;

import com.neusoft.mapper.CreditMapper;
import com.neusoft.po.Credit;
import com.neusoft.service.CreditService;
import com.neusoft.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CreditServiceImpl implements CreditService {

    @Autowired
    CreditMapper creditMapper;

    @Override
    @Transactional
    public int updateCredit(Credit credit) {
        if(credit.getNum()<0){
            String userId=credit.getUserId();
            List<Credit> list=getCreditByUserId(userId);
            int num=-1*credit.getNum();
            if(num>getTotalCreditByUserId(userId)) return 0;
            for(Credit c:list){
                if(num<c.getNum()){

                    //深拷贝并split
                    Credit newAvaiCredit =new Credit();
                    newAvaiCredit.setUserId(userId);
                    newAvaiCredit.setNum(c.getNum()-num);
                    newAvaiCredit.setChangeTime(c.getChangeTime());
                    newAvaiCredit.setType(c.getType());
                    newAvaiCredit.setExpire_time(c.getExpire_time());
                    newAvaiCredit.setStatus(c.getStatus());

                    creditMapper.updateCredit(newAvaiCredit);

                    consumeCredit(c,num);
                    break;
                }
                else if(num==c.getNum()){
                    consumeCredit(c,num);
                    break;
                }
                else{
                    consumeCredit(c,c.getNum());
                    num-=c.getNum();
                }
            }
        }
        return creditMapper.updateCredit(credit);
    }

    @Override
    public List<Credit> getCreditByUserId(String userId) {
        return creditMapper.getCreditByUserId(userId,CommonUtil.getNow());
    }

    @Override
    public List<Credit> getExpiredPoints(String userId) {
        return creditMapper.getExpiredPoints(userId,CommonUtil.getNow());
    }

    @Override
    public int getTotalCreditByUserId(String userId) {
        return creditMapper.getTotalCreditByUserId(userId,CommonUtil.getNow());
    }

    @Override
    public int updateExpiredPoints() {
        return creditMapper.updateExpiredPoints(CommonUtil.getNow());
    }

    @Override
    public int initCredit(Credit credit) {
        credit.setType("REGISTER");
        credit.setNum(0);
        credit.setChangeTime(CommonUtil.getNow());
        return creditMapper.updateCredit(credit);
    }

    @Override
    public int consumeCredit(Credit credit,int num) {
        creditMapper.deleteCreditByRecordId(credit.getRecordId());

        Credit newUsedCredit =new Credit();
        newUsedCredit.setUserId(credit.getUserId());
        newUsedCredit.setNum(num);
        newUsedCredit.setChangeTime(credit.getChangeTime());
        newUsedCredit.setType("CONSUME");
        newUsedCredit.setExpire_time(credit.getExpire_time());
        newUsedCredit.setStatus(2);

        return creditMapper.updateCredit(newUsedCredit);
    }
}
